import { _decorator, Component, Node, v3, Vec3 } from 'cc';
import { WalkNode } from '../GameManager';
import { ZTool } from '../ZTool';
const { ccclass, property } = _decorator;

@ccclass('NPCController')
export class NPCController extends Component implements WalkNode {
    public Path: Vec3[];
    public IsOnPathWalkMove: boolean = false;//打开后会根据路径点去移动
    public Speed: number = 2;
    public _Speed: number = 2;//默认速度
    protected onLoad(): void {
        this._Speed = this.Speed;
    }
    protected start(): void {

    }
    protected update(dt: number): void {
        if (this.IsOnPathWalkMove && this.Path.length > 0) {
            let _v2: Vec3 = v3(this.Path[0].x, this.Path[0].y, this.Path[0].z);
            let _v3 = _v2.subtract(this.node.position);
            this.node.forward = v3(_v3.x, 0, _v3.z);
            this.node.setPosition(this.node.position.clone().add(v3(this.Speed * _v3.normalize().x * dt, this.Speed * _v3.normalize().y * dt, this.Speed * _v3.z * dt)));
            if (this.GetTwoPosXZDis(this.node.position, this.Path[0]) < 0.02) {
                this.Path.splice(0, 1);
                if (this.Path.length == 0) {
                    this.IsOnPathWalkMove = false;
                }
            }
        }
        this.SetAnimation();

    }

    public GetTwoPosXZDis(pos1: Vec3, pos2: Vec3) {
        const x = pos1.x - pos2.x;
        const z = pos1.z - pos2.z;
        return Math.sqrt(x * x + z * z);
    }
    //追玩家
    PursuePlayer() {


    }
    //设置动画状态
    SetAnimation() {


    }
}


